package com.lanou.zy.projecta.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.application.BaseApplication;
import com.lanou.zy.projecta.entity.CountryMsg;
import com.lanou.zy.projecta.entity.OtherCountry;
import com.lanou.zy.projecta.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/10/28.
 */
public class CountryGridViewAdapter extends BaseAdapter {

    private Context context;
    private List<CountryMsg.DataEntity.HotCityEntity> hotcity;
    private List<OtherCountry.DataEntity.LocalDiscountEntity> localDiscountEntities;
    private ImageLoader imageLoader;
    private BaseApplication app;

    public CountryGridViewAdapter(Context c) {
        imageLoader = VolleySingleton.getInstance().getImageLoader();
        this.context = c;
        this.app = (BaseApplication) ((Activity)context).getApplication();
    }

    @Override
    public int getCount() {
        
        if (app.isType()) {
            return localDiscountEntities != null && localDiscountEntities.size() > 0 ? localDiscountEntities.size() : 0;
        } else {
            return hotcity != null && hotcity.size() > 0 ? hotcity.size() : 0;
        }
    }

    @Override
    public Object getItem(int position) {
        if (app.isType()) {
            return localDiscountEntities.get(position);
        } else {
            return hotcity.get(position);
        }
    }

    public void addDatas(List<OtherCountry.DataEntity.LocalDiscountEntity> datas) {
        this.localDiscountEntities = datas;
        notifyDataSetChanged();
    }

    public void addData(List<CountryMsg.DataEntity.HotCityEntity> datas) {
        hotcity = datas;
        notifyDataSetChanged();
    }

    @Override
    public long getItemId(int position) {

        return position;
    }


    @Override
    public View getView(int position, View v, ViewGroup parent) {
        CountryViewHolder holder = null;
        if (app.isType()) {
            if (v == null) {
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                v = inflater.inflate(R.layout.city_gridview_item, null);
                holder = new CountryViewHolder(v);
                v.setTag(holder);
            } else {
                holder = (CountryViewHolder) v.getTag();
            }
            OtherCountry.DataEntity.LocalDiscountEntity odl = localDiscountEntities.get(position);
            holder.chmContent_Tv.setText(odl.getTitle());
            holder.chmPrice_Tv.setText(odl.getPrice().replace("<em>", "").replace("</em>", ""));
            holder.chmPriceOff_Tv.setText(odl.getPriceoff());
            holder.cityHotMguide_Iv.setImageUrl(odl.getPhoto(), imageLoader);
            return v;

        } else {

            if (v == null) {
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                v = inflater.inflate(R.layout.country_gridview_item, null);
                holder = new CountryViewHolder(v);
                v.setTag(holder);
            } else {
                holder = (CountryViewHolder) v.getTag();
            }
            CountryMsg.DataEntity.HotCityEntity cdh = hotcity.get(position);
            holder.country_Hot_City_Cnnname_Tv.setText(cdh.getCnname());
            holder.country_Hot_City_Ennname_Tv.setText(cdh.getEnname());
            holder.country_Hot_City_Iv.setImageUrl(cdh.getPhoto(), imageLoader);
            return v;
        }


    }

//    private void getBitmapCache(String url, ImageView imageView) {
//        ImageLoader.ImageListener listener = ImageLoader.getImageListener(
//                imageView, R.mipmap.ic_qyer_gray_150, R.mipmap.ic_qyer_gray_150
//        );
//        imageLoader.get(url, listener);
//    }


    public class CountryViewHolder {
        private TextView country_Hot_City_Cnnname_Tv, country_Hot_City_Ennname_Tv;
        private NetworkImageView cityHotMguide_Iv , country_Hot_City_Iv;
        private TextView chmContent_Tv, chmPriceOff_Tv, chmPrice_Tv;

        public CountryViewHolder(View v) {
            if (app.isType()) {
                cityHotMguide_Iv = (NetworkImageView) v.findViewById(R.id.cityHotMguide_Iv);
                chmContent_Tv = (TextView) v.findViewById(R.id.chmContent_Tv);
                chmPriceOff_Tv = (TextView) v.findViewById(R.id.chmPriceOff_Tv);
                chmPrice_Tv = (TextView) v.findViewById(R.id.chmPrice_Tv);
            } else {
                country_Hot_City_Iv = (NetworkImageView) v.findViewById(R.id.country_Hot_City_Iv);
                country_Hot_City_Cnnname_Tv = (TextView) v.findViewById(R.id.country_Hot_City_Cnnname_Tv);
                country_Hot_City_Ennname_Tv = (TextView) v.findViewById(R.id.country_Hot_City_Ennname_Tv);
            }
        }
    }


}
